<template>
  <div>
    <el-table
      v-loading="listloading"
      :data="tableData"
      border
      style="width: 100%"
      ref="table"
    >
      <el-table-column prop="email" label="账号" width="200"> </el-table-column>
      <el-table-column prop="name" label="昵称" width="180"> </el-table-column>
      <el-table-column prop="password" label="密码" width="580">
      </el-table-column>
      <el-table-column prop="createDate" label="注册时间"> </el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        width="100"
        :filters="[
          { text: '正常', value: '正常' },
          { text: '锁定', value: '锁定' },
        ]"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.state == '正常' ? 'primary' : 'danger'"
            disable-transitions
            >{{ scope.row.state }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row)"
            type="text"
            size="small"
            style="margin-right: 20px"
            >修改
          </el-button>
          <el-button type="text" size="small" @click="delFn(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="信息修改" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form">
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.state">
            <el-radio label="正常"></el-radio>
            <el-radio label="锁定"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "user",
  data() {
    return {
      tableData: [],
      listloading: false,
      dialogFormVisible: false,
      form: {
        id: "",
        name: "",
        state: "",
      },
      formLabelWidth: "100px",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listloading = true;
      this.$https.get("/user/all").then((res) => {
        this.listloading = false;
        let data = res.data;
        data.forEach((arr) => {
          arr.state = arr.state == 1 ? "正常" : "锁定";
        });
        this.tableData = data;
      });
    },
    handleClick(row) {
      this.form.id = row.id;
      this.form.name = row.name;
      this.form.state = row.state;
      this.dialogFormVisible = true;
    },
    saveFn() {
      this.form.state = this.form.state == "正常" ? 1 : 0;
      this.$https.post("/user/update", this.form).then((res) => {
        this.dialogFormVisible = false;
        if (res.code == 200) {
          this.getList();
        }
      });
    },
    delFn(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$https.delete("/user/deluser", { params: { id } }).then((res) => {
              if (res.code == 200) {
                this.getList();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
